package com.jspgou.cms.entity;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.jspgou.cms.entity.base.BaseBrand;

public class Brand extends BaseBrand {
	private static final long serialVersionUID = 1L;

	/**
	 * 获得图片URL
	 * 
	 * @return
	 */
	public String getLogoUrl() {
		String path = getLogoPath();
		if (StringUtils.isBlank(path)) {
			return null;
		} else {
			return getWebsite().getUploadUrl(path);
		}
	}

	/**
	 * 获得品牌详细信息
	 * 
	 * @return
	 */
	public String getText() {
		BrandText brandText = getBrandText();
		if (brandText != null) {
			return brandText.getText();
		}
		return null;
	}

	public BrandText getBrandText() {
		Set<BrandText> set = getBrandTextSet();
		if (set != null) {
			Iterator<BrandText> it = set.iterator();
			if (it.hasNext()) {
				return it.next();
			}
		}
		return null;
	}

	/**
	 * 添加至types
	 * 
	 * @param type
	 */
	public void addToTypes(ProductType type) {
		Set<ProductType> set = getTypes();
		if (set == null) {
			set = new HashSet<ProductType>();
			setTypes(set);
		}
		set.add(type);
	}

	/**
	 * 从types中删除
	 * 
	 * @param type
	 */
	public void removeFromTypes(ProductType type) {
		Set<ProductType> set = getTypes();
		if (set != null) {
			set.remove(type);
		}
	}
	
	/**
	 * 从categorys中删除(wang ze wu)
	 * 
	 * @param type
	 */
	public void removeFromCategorys(Category category) {
		Set<Category> set = this.getCategorys();
		if (set != null) {
			set.remove(category);
		}
	}
	
	/**
	 * 添加至categorys(wang ze wu)
	 * 
	 * @param type
	 */
	public void addToCategory(Category category) {
		Set<Category> set = getCategorys();
		if (set == null) {
			set = new HashSet<Category>();
			this.setCategorys(set);
		}
		set.add(category);
	}

	/**
	 * 获得商品类型ID数组
	 * 
	 * @return
	 */
	public Long[] getTypeIds() {
		Set<ProductType> set = getTypes();
		if (set == null) {
			return null;
		} else {
			Long[] ids = new Long[set.size()];
			int i = 0;
			for (ProductType type : set) {
				ids[i] = type.getId();
				i++;
			}
			return ids;
		}
	}

	/* [CONSTRUCTOR MARKER BEGIN] */
	public Brand () {
		super();
	}

	/**
	 * Constructor for primary key
	 */
	public Brand (java.lang.Long id) {
		super(id);
	}

	/**
	 * Constructor for required fields
	 */
	public Brand (
		java.lang.Long id,
		com.jspgou.core.entity.Website website,
		java.lang.String name,
		java.lang.Integer priority) {

		super (
			id,
			website,
			name,
			priority);
	}

	/* [CONSTRUCTOR MARKER END] */

}